package java67_adv7;

import java.io.File;
import java.util.*;

public class SearchFile {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入查找的文件夹名：");
		String path = in.next();
		// showFile(new File(path));

		System.out.println(getFileSize(new File(path)));
	}

	public static void showFile(File f) {
		LinkedList<File> files = new LinkedList<>();
		files.add(f);
		while (files.size() > 0) {
			File t = files.removeFirst();
			if (t.canRead() == false)// 防止权限不够
				continue;

			System.out.println(t.getAbsolutePath());
			if (f.isDirectory()) {
				File[] fr = t.listFiles();
				if (fr != null && fr.length > 0)
					Collections.addAll(files, fr);
			}
		}
	}

	public static long getFileSize(File f) {
		long size = 0L;
		LinkedList<File> files = new LinkedList<>();
		files.add(f);
		while (files.size() > 0) {
			File t = files.removeFirst();
			if (t.canRead() == false)// 防止权限不够
				continue;
			System.out.println(t.getAbsolutePath() + "," + f.isDirectory());
			// if (f.isDirectory() == false)
			size += t.length();

			if (f.isDirectory()) {
				File[] fr = t.listFiles();
				if (fr != null && fr.length > 0)
					Collections.addAll(files, fr);
			}
		}
		return size;
	}

}
